package com.iot08.api.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author weichao 1299101858@qq.com
 * @version 1.0
 * @date 12/16/21 2:15 PM
 */
@EnableAsync
@Configuration
public class TaskPoolConfig {
//    @Bean(name = AsyncExecutionAspectSupport.DEFAULT_TASK_EXECUTOR_BEAN_NAME)
//    public Executor taskExecutor() {
//        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//        //核心线程池大小
//        executor.setCorePoolSize(10);
//        //最大线程数
//        executor.setMaxPoolSize(20);
//        //队列容量
//        executor.setQueueCapacity(200);
//        //活跃时间
//        executor.setKeepAliveSeconds(60);
//        //线程名字前缀
//        executor.setThreadNamePrefix("taskExecutor-");
//        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//        return executor;
//    }

    // 恢复出厂设置的线程池
    @Bean(name = "resetDev_Task")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心线程池大小
        executor.setCorePoolSize(500);
        //最大线程数
        executor.setMaxPoolSize(1000);
        //队列容量
        executor.setQueueCapacity(10000);
        //活跃时间
        executor.setKeepAliveSeconds(60);
        //线程名字前缀
        executor.setThreadNamePrefix("resetDev_Task-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }
}
